#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct People
{
	string name;
	int year;
	int month;
	int day;
}people[1000];
int cmp(struct People a,struct People b)
{
	if(a.year < b.year) return 1;
	else if(a.year == b.year && a.month < b.month) return 1;
	else if(a.year == b.year && a.month == b.month && a.day < b.day) return 1;
	else return 0;
}
int main()
{
	freopen("in.txt","r",stdin);
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		cin >> people[i].name >> people[i].year >> people[i].month >> people[i].day;
	}
	int i,j;
	for(i = n-2; i>= 0; i--)
	{
		if(cmp(people[i],people[i+1]))
		{
			people[n] = people[i];
			for(j = i+1; cmp(people[n],people[j]);j++)
			{
				people[j-1] = people[j];
			}
			people[j-1] = people[n];
		}
	}
	for(int i = n-1; i >= 0; i--)
	{
		cout << people[i].name << endl;
	}
}
